package com.example.getinputheight;

import com.example.getinputheight.KeyboardDetectorRelativeLayout.OnSoftKeyboardListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
	KeyboardDetectorRelativeLayout root_layout ;
	EditText et;
	Button btn;
	InputContentPopupWindon popu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.btn_h);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

		 		InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
				manager.showSoftInput(v, InputMethodManager.RESULT_SHOWN);
				manager.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);


			}

		});
		root_layout = (KeyboardDetectorRelativeLayout) findViewById(R.id.root_layout);

		root_layout.setOnSoftKeyboardListener(new OnSoftKeyboardListener() {

			@Override
			public void onShown(int keyboardHeight) {
				// TODO Auto-generated method stub
				showPopupwin();
			}

			@Override
			public void onMeasureFinished() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onHidden() {
				// TODO Auto-generated method stub
				if(popu!=null){
					popu.dismiss();
				}
			}
		});
	}


	public void showPopupwin(){
		popu = new InputContentPopupWindon(this, listener);
		popu.setOutsideTouchable(false);//点击屏幕意外位置消失
		popu.setFocusable(true);
		SharedPreferences pref = this.getSharedPreferences("in_put", Context.MODE_PRIVATE);
		int size = pref.getInt("size", 0);
		Log.i("TAG", "输入框的高度为=="+size);
		popu.showAtLocation(this.findViewById(R.id.root_layout), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, size);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//			popu.dismiss();
			switch (v.getId()) {
			case R.id.send_btn:
				Toast.makeText(MainActivity.this, ((EditText)(popu.getContentView().findViewById(R.id.input_et))).getText().toString(), 0).show();
				v.requestFocus();
				InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
				manager.hideSoftInputFromWindow(v.getWindowToken(), 0); //隐藏
				break;

			default:
				break;
			}
		}
	};

	protected void onPause() {
		super.onPause();
		InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
		//强制隐藏
		manager.hideSoftInputFromWindow(popu.getContentView().findViewById(R.id.input_et).getWindowToken(), 0);
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(popu!=null){
			popu.dismiss();
		}
	}


}
